﻿using Mrc.ConfigCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mrc.Config
{
    public class MrcConfigOptions
    {
        public MrcConfigOptions()
        {
            Extensions = new List<IMrcConfigOptionsExtension>();
            ItemName = "ItemName";
            Version = "v1";
            HostId = IdService.CreateSnowflakeId().ToString();
        }

        internal IList<IMrcConfigOptionsExtension> Extensions { get; }

        public string ItemName { get; set; }

        public string MasterHost { get; set; }

        public string Version { get; set; }

        public string HostId { get; set; }

        public Dictionary<string, ConfigFileInfo> ConfigFileInfos { get; set; } = new Dictionary<string, ConfigFileInfo>(StringComparer.OrdinalIgnoreCase);

        public void AddConfig(string key, string path)
        {
            if (!ConfigFileInfos.ContainsKey(key))
            {
                ConfigFileInfos.Add(key, new ConfigFileInfo { Name = key, Path = path });
            }
        }
        public void AddConfigs(Dictionary<string, string> files)
        {
            foreach (var item in files)
            {
                if (!ConfigFileInfos.ContainsKey(item.Key))
                {
                    ConfigFileInfos.Add(item.Key, new ConfigFileInfo { Name = item.Key, Path = item.Value });
                }
            }
        }
        public void RegisterExtension(IMrcConfigOptionsExtension extension)
        {
            if (extension == null)
            {
                throw new ArgumentNullException(nameof(extension));
            }
            Extensions.Add(extension);
        }
    }
}
